package com.yanfan.energy.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import javax.validation.Valid;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.yanfan.common.utils.DateUtils;
import com.yanfan.common.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yanfan.common.annotation.Log;
import com.yanfan.common.core.controller.BaseController;
import com.yanfan.common.core.domain.AjaxResult;
import com.yanfan.common.enums.BusinessType;
import com.yanfan.energy.domain.NjEnergyDeviceData;
import com.yanfan.energy.service.INjEnergyDeviceDataService;
import com.yanfan.common.utils.poi.ExcelUtil;
import com.yanfan.common.core.page.TableDataInfo;

/**
 * 能耗设备数据Controller
 *
 * @author genluo
 * @date 2024-10-29
 */
@Api(tags = "能耗设备数据")
@RestController
@RequestMapping("/energy/deviceData")
public class NjEnergyDeviceDataController extends BaseController {
    @Autowired
    private INjEnergyDeviceDataService njEnergyDeviceDataService;

    /**
     * 查询能耗设备数据列表
     */
    @ApiOperation("能耗设备数据分页列表")
    @PreAuthorize("@ss.hasPermi('energy:deviceData:list')")
    @GetMapping("/list")
    public TableDataInfo list(NjEnergyDeviceData njEnergyDeviceData) {
        startPage();
        List<NjEnergyDeviceData> list = njEnergyDeviceDataService.queryPageList(njEnergyDeviceData);
        return getDataTable(list);
    }

    /**
     * 导出能耗设备数据列表
     */
    @ApiOperation("导出能耗设备数据列表")
    @PreAuthorize("@ss.hasPermi('energy:deviceData:export')")
    @Log(title = "能耗设备数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, NjEnergyDeviceData njEnergyDeviceData) {
        List<NjEnergyDeviceData> list = njEnergyDeviceDataService.queryPageList(njEnergyDeviceData);
        ExcelUtil<NjEnergyDeviceData> util = new ExcelUtil<NjEnergyDeviceData>(NjEnergyDeviceData.class);
        util.exportExcel(response, list, "能耗设备数据数据");
    }

    /**
     * 获取能耗设备数据详细信息
     */
    @ApiOperation("获取能耗设备数据详细信息")
    @PreAuthorize("@ss.hasPermi('energy:deviceData:query')")
    @GetMapping(value = "/{deviceName}")
    public AjaxResult getInfo(@PathVariable("deviceName") String deviceName) {
        return success(njEnergyDeviceDataService.getById(deviceName));
    }

    /**
     * 新增能耗设备数据
     */
    @ApiOperation("新增能耗设备数据")
    @PreAuthorize("@ss.hasPermi('energy:deviceData:add')")
    @Log(title = "能耗设备数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody NjEnergyDeviceData njEnergyDeviceData) {
        return toAjax(njEnergyDeviceDataService.save(njEnergyDeviceData));
    }

    /**
     * 修改能耗设备数据
     */
    @ApiOperation("修改能耗设备数据")
    @PreAuthorize("@ss.hasPermi('energy:deviceData:edit')")
    @Log(title = "能耗设备数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody NjEnergyDeviceData njEnergyDeviceData) {
        njEnergyDeviceData.setUpdateTime(DateUtils.getTime());
        return toAjax(njEnergyDeviceDataService.updateById(njEnergyDeviceData));
    }

    /**
     * 删除能耗设备数据
     */
    @ApiOperation("删除能耗设备数据")
    @PreAuthorize("@ss.hasPermi('energy:deviceData:remove')")
    @Log(title = "能耗设备数据", businessType = BusinessType.DELETE)
    @DeleteMapping("/{deviceNames}")
    public AjaxResult remove(@PathVariable String[] deviceNames) {
        return toAjax(njEnergyDeviceDataService.removeBatchByIds(Arrays.asList(deviceNames)));
    }


    /**
     * 大屏能耗数据分楼栋展示
     * @return
     */
    @ApiOperation("大屏能耗数据分楼栋展示")
    @GetMapping(value = "/bigScreen")
    public AjaxResult getBigScreenData() {
        JSONObject jsonObject = new JSONObject();
        QueryWrapper<NjEnergyDeviceData> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("building", "SUM(value) as sumValue")
                .eq("device_type", "电表")
                .groupBy("building");
        List<Map<String, Object>>  dian= njEnergyDeviceDataService.listMaps(queryWrapper);

        QueryWrapper<NjEnergyDeviceData> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.select("building", "SUM(value) as sumValue")
                .eq("device_type", "水表")
                .groupBy("building");
        List<Map<String, Object>>  water= njEnergyDeviceDataService.listMaps(queryWrapper1);
        jsonObject.put("dian",dian);
        jsonObject.put("water",water);
        return success(jsonObject);
    }


}
